//
//  CODetailListController.m
//  Runone
//
//  Created by runone01 on 16/8/11.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "CODetailListController.h"
#import "CODeatilListCell.h"
#import "CO2COSetting.h"
#import "DeviceBaseInfo.h"
#import "VideoSetting.h"
#import "PopView.h"
#import "LewPopupViewController.h"

#import "DeviceBaseInfo.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "DMCOVICurrentDataInfo.h"
#import "UIView+Message.h"

#import "COVIViewController.h"

@interface CODetailListController ()<UITableViewDataSource, UITableViewDelegate,UIPopoverPresentationControllerDelegate>
@property (strong, nonatomic)DeviceBaseInfo *Camera;
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)DMCOVICurrentDataInfo *CO;
@property (strong, nonatomic)PopView *popV;
@property (strong, nonatomic)COVIViewController *coVC;

@end
static NSString *const cellId =@"cell";
@implementation CODetailListController{
    UITableView *coTalbieView;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"COVI";
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text=@"COVI";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=label;
    coTalbieView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    
    coTalbieView.dataSource = self;
    coTalbieView.delegate   = self;
    coTalbieView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [coTalbieView registerNib:[UINib nibWithNibName:NSStringFromClass([CODeatilListCell class]) bundle:nil] forCellReuseIdentifier:cellId];
    coTalbieView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:coTalbieView];
   
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}
-(void)request
{
    
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetDeviceDataURL;
    getCurrentEventModel.DataValue= [self DataValueStr:_Camera.DeviceUID];
    getCurrentEventModel.UpdatedTime=@"";
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"GetDeviceSettingInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        NSLog(@"%@",requestOutginseng.DataValue);
         _coVC=[[COVIViewController alloc] init];
        _contentView=MessageViewFrame;
        NSMutableArray *mutable=[[NSMutableArray alloc] init];
        NSDictionary *dic=[requestOutginseng.DataValue mj_JSONObject];
        NSDictionary *info = [[dic objectForKey:@"DeviceData"] mj_JSONObject];
        if (info) {
            _CO=[DMCOVICurrentDataInfo mj_objectWithKeyValues:info];
            [mutable addObject:_CO];
        }else
        {
            [_contentView prompt:@" CO暂无内容!~ "];
            [self.view addSubview:_contentView];
            return ;
        }
        _coVC.dataSource=mutable;
        
        if (_CO) {
            [self picker];
        }
        else
        {
            [_contentView prompt:@" CO暂无内容!~ "];
            [self.view addSubview:_contentView];
            return ;
        }
//        [_contentView DeviceMessage:[NSString stringWithFormat:@"CO数据值:%@\nVI数据值:%@\n最后更新时间:%@\nCO状态:       %@\nVI状态:        %@\n",_CO.CO_DATA,_CO.VI_DATA,_CO.LAST_UPDATE,_CO.CO_STATUS,_CO.VI_STATUS]];
        
    };
    [requestHelper RequestGinsengModel:getCurrentEventModel andPushController:self];
}

-(void)picker {
    _coVC.modalPresentationStyle=UIModalPresentationPopover;
    _coVC.popoverPresentationController.sourceView=self.view;
    _coVC.popoverPresentationController.sourceRect=CGRectMake(self.view.center.x, self.view.center.y, 0, 0);
    _coVC.popoverPresentationController.permittedArrowDirections=NO;
    _coVC.popoverPresentationController.delegate=self;
    [self presentViewController:_coVC animated:YES completion:nil];
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    _contentView.hidden=YES;
    [_contentView removeFromSuperview];
}
-(NSString *)DataValueStr:(NSString *)str {
    NSString *beginstr=[NSString stringWithFormat:@"[{\"key\":\"DeviceUID\",\"value\":\"%@\"}]",str];
    
    return beginstr;
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CODeatilListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[CODeatilListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    DeviceBaseInfo *baseInfo=self.contentArray[indexPath.row];
    
    cell.pileNOLabel.text = [NSString stringWithFormat:@"桩号：%@+%ld",baseInfo.PileNo,(long)baseInfo.PileDistance];
    if (baseInfo.Direction == 1) {
        cell.DerectionLabel.text = [NSString stringWithFormat:@"往湖南方向"];
    }else {
        cell.DerectionLabel.text = [NSString stringWithFormat:@"往广州方向"];
    }
    //    cell.CO2ConcentrationLabel.text = [NSString stringWithFormat:@"CO2浓度：%ld",(long)co.CO2ConcentrationValue];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _Camera = self.contentArray[indexPath.row];
    [self request];
}


@end
